package model;

import dao.EmailDao;
import dao.DaoFactory;
import java.util.HashMap;
import java.util.Map;
import java.sql.Timestamp;

/**
 * 
 * This class is Email model class
 * 
 * @author Sukhjinder K. Narula (narula.sk@gmail.com)
 * @since May 2013
 */
public class Email {
	private final EmailDao emailDao;
	private int id;
	private String to;
	private String from;
	private String subject;
	private String body;
	private boolean read;
	private Timestamp timestamp;
	private int folderId;

	/**
	 * Constructor
	 * 
	 * @param id
	 * @param to
	 * @param from
	 * @param subject
	 * @param body
	 * @param read
	 * @param timestamp
	 * @param folderId
	 */
	public Email(String to, String from, String subject, String body, boolean read, Timestamp timestamp, int folderId) {
		this.to = to;
		this.from = from;
		this.subject = subject;
		this.body = body;
		this.read = read;
		this.timestamp = timestamp;
		this.folderId = folderId;
		this.emailDao = DaoFactory.getEmailDao();		
	}

	/**
	 * 
	 * @return id
	 */
	public int getId() {
		return id;
	}

	/**
	 * 
	 * @param id
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * 
	 * @return to
	 */
	public String getTo() {
		return to;
	}

	/**
	 * 
	 * @param to
	 */
	public void setTo(String to) {
		this.to = to;
	}

	/**
	 * 
	 * @return from
	 */
	public String getFrom() {
		return from;
	}

	/**
	 * 
	 * @param from
	 */
	public void setFrom(String from) {
		this.from = from;
	}

	/**
	 * 
	 * @return subject
	 */
	public String getSubject() {
		return subject;
	}

	/**
	 * 
	 * @param subject
	 */
	public void setSubject(String subject) {
		this.subject = subject;
	}

	/**
	 * 
	 * @return body
	 */
	public String getBody() {
		return body;
	}

	/**
	 * 
	 * @param body
	 */
	public void setBody(String body) {
		this.body = body;
	}

	/**
	 * 
	 * @return read
	 */
	public boolean isRead() {
		return read;
	}

	/**
	 * 
	 * @param read
	 */
	public void setRead(boolean read) {
		this.read = read;
	}

	/**
	 * 
	 * @return timestamp
	 */
	public Timestamp getTimestamp() {
		return timestamp;
	}

	/**
	 * 
	 * @param timestamp
	 */
	public void setTimestamp(Timestamp timestamp) {
		this.timestamp = timestamp;
	}

	/**
	 * 
	 * @return folderId
	 */
	public int getFolderId() {
		return folderId;
	}

	/**
	 * 
	 * @param folderId
	 */
	public void setFolderId(int folderId) {
		this.folderId = folderId;
	}

	/**
	 * 
	 * @return boolean
	 */
	public boolean insert() {
		try {
			this.id = emailDao.insert(createProperties());
		} catch (Exception e) {
			return false;
		}
			return true;
	}
	
	/**
	 * 
	 * @return boolean
	 */
	public boolean select(int id) {
			Map<String, String> emailProperties = null;
			try {
				emailProperties = emailDao.select(id);
			} catch (Exception e) {
				return false;
			}
			this.id = id;
			this.to = emailProperties.get(to);
			this.from = emailProperties.get(from);
			this.subject = emailProperties.get(subject);
			this.body = emailProperties.get(body);
			this.read = Boolean.valueOf(emailProperties.get(read));
			this.timestamp = Timestamp.valueOf(emailProperties.get(timestamp));
			this.folderId = Integer.parseInt(emailProperties.get(folderId));
			
			return true;
	}
	
	/**
	 * 
	 * @return boolean
	 */
	public boolean update() {
		try {
			this.emailDao.update(createProperties());
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 * 
	 * @param newFolderId
	 */
	public boolean updateFolder() {
		try {
			this.emailDao.updateFolder(createProperties());
		} catch (Exception e) {
			return false;
		}
			return true;
	}

	/**
	 * Delete the email from the db
	 */
	public boolean delete() {
		try {
			this.emailDao.delete(this.getId());
		} catch (Exception e) {
			return false;
		}
			return true;
	}

	/**
	 * @return String
	 */
	public String toString() {
		return "";
	}

	/**
	 * 
	 * @return properties
	 */
	@SuppressWarnings("serial")
	private Map<String, String> createProperties() {
		return new HashMap<String, String>() {
			{
				put("id", String.valueOf(id));
				put("to", getTo());
				put("from", getFrom());
				put("subject", getSubject());
				put("body", getBody());
				put("read", String.valueOf(isRead()));
				put("timestamp", String.valueOf(getTimestamp()));
				put("folderId", String.valueOf(getFolderId()));
			}
		};
	}
}
